var e = require("../../@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", { value: !0 }),
  (exports.default = void 0);
var t = require("../../@babel/runtime/helpers/regeneratorRuntime"),
  n = require("../../@babel/runtime/helpers/asyncToGenerator"),
  o = require("../../@babel/runtime/helpers/classCallCheck"),
  r = require("../../@babel/runtime/helpers/createClass"),
  i = require("../../@babel/runtime/helpers/inherits"),
  s = require("../../@babel/runtime/helpers/createSuper"),
  c = e(require("../common/EventDispatcher")),
  a = require("../api/C2API"),
  u = e(require("../BLEPeripheral/HxBLEPeripheral")),
  l = e(require("../lib/mqtt.min.js")),
  h = require("../common/user"),
  m = e(require("../common/common")),
  f = (function (e) {
    i(_, e);
    var c,
      f = s(_);
    function _() {
      var e;
      return (
        o(this, _),
        ((e = f.call(this))._aoaBroker = void 0),
        (e.keepalive = 2),
        (e.reconnectPeriod = 1e4),
        (e.connectTimeout = 5e3),
        (e.version = 5),
        e
      );
    }
    return (
      r(_, [
        {
          key: "init",
          value:
            ((c = n(
              t().mark(function e(n) {
                var o, r, i;
                return t().wrap(
                  function (e) {
                    for (;;)
                      switch ((e.prev = e.next)) {
                        case 0:
                          if (
                            ((o = n.accessCode),
                            (r = n.aoaBroker),
                            (0, h.getUserId)())
                          ) {
                            e.next = 4;
                            break;
                          }
                          return (e.next = 4), (0, h.genThirdUserId)(o);
                        case 4:
                          return (
                            (e.next = 6),
                            a.C2API.getAoAMac(o, (0, h.getUserId)())
                          );
                        case 6:
                          (i = e.sent),
                            u.default.setMac(i),
                            (this._mac = i),
                            (this._accessCode = o),
                            (this._aoaBroker = r),
                            (this._sysMac = "43544d41" + this._mac),
                            (this._reconnectTimer = void 0),
                            (this._responseTime = Date.now());
                        case 14:
                        case "end":
                          return e.stop();
                      }
                  },
                  e,
                  this,
                );
              }),
            )),
            function (e) {
              return c.apply(this, arguments);
            }),
        },
        {
          key: "start",
          value: function () {
            this._mac
              ? (u.default.start(), this.connect())
              : console.error("aoa mac undefined");
          },
        },
        {
          key: "closeWs",
          value: function () {
            clearInterval(this._reconnectTimer),
              this._ws &&
                (this._ws.removeAllListeners(),
                this._ws.end(!0),
                (this._ws = null));
          },
        },
        {
          key: "stop",
          value: function () {
            this.closeWs(), u.default.stop();
          },
        },
        {
          key: "getAoAPeri",
          value: function () {
            return u.default;
          },
        },
        {
          key: "getClientId",
          value: function () {
            return (
              this._clientId ||
                (this._clientId = this._mac + "_" + Date.now().toString(32)),
              this._clientId
            );
          },
        },
        {
          key: "connect",
          value: function () {
            var e = this;
            console.log(m.default.getTimeStr(), "开始连接aoa mqtt"),
              this.closeWs(),
              (this._responseTime = Date.now()),
              this.emit("aoa_mqtt_event", {
                timestamp: Date.now(),
                event: "connect",
              }),
              (this._reconnectTimer = setInterval(function () {
                Date.now() - e._responseTime > 1e3 * e.keepalive + 1500 &&
                  (console.error(m.default.getTimeStr(), "mqtt timeout"),
                  e.connect());
              }, 1e3)),
              (this._connectStatus = !1);
            var t = this._aoaBroker;
            (this._ws = l.default.connect(t, {
              protocolVersion: this.version,
              clean: !1,
              properties: { sessionExpiryInterval: 30 },
              resubscribe: !0,
              keepalive: this.keepalive,
              reconnectPeriod: this.reconnectPeriod,
              connectTimeout: this.connectTimeout,
              clientId: this.getClientId(),
            })),
              this._ws.on("connect", function () {
                (e._responseTime = Date.now()),
                  console.log(m.default.getTimeStr(), "mqtt 连接成功a。。。。"),
                  console.info(e._mac, "macId..."),
                  (e._connectStatus = !0);
                var t = "location/src/"
                  .concat(e._accessCode, "/")
                  .concat(e._sysMac.toUpperCase());
                console.info(t, "订阅地址"), e._ws.subscribe(t);
              }),
              this._ws.on("message", function (t, n) {
                var o = JSON.parse(n.toString());
                if (o.aoa) {
                  var r = o.aoa;
                  (r.height = r.z),
                    (r.accuray = 1),
                    e.emit("aoa_positionChanged", { value: r });
                }
              }),
              this._ws.on("close", function () {
                e._connectStatus = !1;
              }),
              this._ws.on("error", function (e) {
                console.info("mqtt error", e);
              }),
              this._ws.on("reconnect", function () {
                console.info("mqtt reconnect");
              }),
              this._ws.on("disconnect", function () {
                console.info("mqtt disconnect");
              }),
              this._ws.on("offline", function () {
                console.info("mqtt offline");
              }),
              this._ws.on("packetreceive", function (t) {
                e._responseTime = Date.now();
              });
          },
        },
      ]),
      _
    );
  })(c.default);
exports.default = f;
